package org.example.curriculum_design_6_16.controller;

import org.example.curriculum_design_6_16.entity.pojo.IntellectualProperty;
import org.example.curriculum_design_6_16.entity.vo.IntellectualPropertyVo;
import org.example.curriculum_design_6_16.entity.vo.Result;
import org.example.curriculum_design_6_16.service.IntellectualPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/intellectualProperty")
public class IntellectualPropertyController {
    @Autowired
    private IntellectualPropertyService intellectualPropertyService;

    @PostMapping
    public Result<Integer> addIntellectualProperty(@RequestBody IntellectualProperty intellectualProperty) {
        try {
            System.out.println("接收到的知识产权数据: " + intellectualProperty);
            int result = intellectualPropertyService.addIntellectualProperty(intellectualProperty);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加知识产权失败: " + e.getMessage());
        }
    }

    @PutMapping
    public Result<Integer> updateIntellectualProperty(@RequestBody IntellectualProperty intellectualProperty) {
        try {
            return Result.success(intellectualPropertyService.updateIntellectualProperty(intellectualProperty));
        } catch (Exception e) {
            return Result.error("更新知识产权失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<Integer> deleteIntellectualProperty(@PathVariable Integer id) {
        try {
            return Result.success(intellectualPropertyService.deleteIntellectualProperty(id));
        } catch (Exception e) {
            return Result.error("删除知识产权失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result<IntellectualPropertyVo> getIntellectualPropertyById(@PathVariable Integer id) {
        try {
            return Result.success(intellectualPropertyService.getIntellectualPropertyById(id));
        } catch (Exception e) {
            return Result.error("获取知识产权记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/student/{studentId}")
    public Result<List<IntellectualPropertyVo>> getIntellectualPropertiesByStudentId(@PathVariable String studentId) {
        try {
            return Result.success(intellectualPropertyService.getIntellectualPropertiesByStudentId(studentId));
        } catch (Exception e) {
            return Result.error("获取学生知识产权记录失败: " + e.getMessage());
        }
    }

    @GetMapping
    public Result<List<IntellectualPropertyVo>> getAllIntellectualProperties() {
        try {
            return Result.success(intellectualPropertyService.getAllIntellectualProperties());
        } catch (Exception e) {
            return Result.error("获取所有知识产权记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/score/total/{studentId}")
    public Result getIntellectualPropertyTotal(@PathVariable String studentId, @RequestParam String academicYear) {
        return Result.success(intellectualPropertyService.getTotalScore(studentId, academicYear));
    }

    @GetMapping("/types")
    public Result<List<String>> getAllTypes() {
        return Result.success(intellectualPropertyService.getAllDistinctIpTypes());
    }

    @GetMapping("/test")
    public Result<String> testConnection() {
        try {
            List<String> types = intellectualPropertyService.getAllDistinctIpTypes();
            return Result.success("数据库连接正常，知识产权类型数量: " + types.size());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("数据库连接失败: " + e.getMessage());
        }
    }
}